package com.punai.costtool.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.punai.common.core.controller.BaseController;
import com.punai.common.core.domain.AjaxResult;
import com.punai.common.core.page.TableDataInfo;
import com.punai.costtool.domain.entity.ToolMaintenance;
import com.punai.costtool.domain.vo.ToolMaintenanceVo;
import com.punai.costtool.service.ToolMaintenanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.ParseException;
import java.util.List;


@RestController
@RequestMapping("/costTool/toolMaintenance")
public class ToolMaintenanceController extends BaseController {

    @Autowired
    private ToolMaintenanceService toolMaintenanceService;

    @PreAuthorize("@ss.hasPermi('costTool:toolMaintenance:list')")
    @PostMapping("/list")
    public TableDataInfo list(ToolMaintenanceVo vo) throws ParseException {
        startPage();
        List<ToolMaintenance> list = toolMaintenanceService.list(Wrappers.lambdaQuery());
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('costTool:toolMaintenance:save')")
    @PostMapping("/save")
    public AjaxResult save(ToolMaintenanceVo vo) {
        toolMaintenanceService.saveOrUpdate(vo);
        return AjaxResult.success();
    }

    @PreAuthorize("@ss.hasPermi('costTool:toolMaintenance:upload')")
    @PostMapping("/uploadFile/{id}")
    public AjaxResult uploadFile(@PathVariable("id") String id, MultipartFile file) {
        try {
            toolMaintenanceService.uploadFile(id,file);
            return AjaxResult.success("上传成功");
        } catch (IOException e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }
}

